package com.hcx.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcx.reggie.pojo.Category;
import com.hcx.reggie.service.CategoryService;
import com.hcx.reggie.utils.R;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("category")
public class CategoryController {


    private CategoryService categoryService;

    @Autowired
    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    /**
     * 获取页面
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public R<Page> page(int page, int pageSize) {
        Page<Category> Ipage = new Page(page, pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);
        categoryService.page(Ipage,wrapper);
        return R.success(Ipage);
    }

    /**
     * 增加菜品种类
     * @param request
     * @param category
     * @return
     */
    @PostMapping()
    public  R<String> addCategory(HttpServletRequest request, @RequestBody Category category){
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateTime(LocalDateTime.now());
//        category.setCreateUser((Long) request.getSession().getAttribute("employee"));
//        category.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        String name = category.getName();
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getName,name);
        Category flag = categoryService.getOne(wrapper);
        if(flag == null) {
            categoryService.save(category);
            return R.success("添加成功！");
        }
        return R.error("该菜品已经添加了awa");
    }

    /**
     * 更新菜品种类
     * @param category
     * @return
     */
    @PutMapping()
    public R<String> update(@RequestBody Category category){
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getName,category.getName());
        Category flag = categoryService.getOne(wrapper);
        if(flag == null) {
            categoryService.updateById(category);
            return R.success("修改成功");
        }
        if(flag.getId() != category.getId()){
            Wrapper<Category> wrapperCate = new UpdateWrapper<>();
            wrapper.eq(Category::getId,flag.getId());

            categoryService.update(category,wrapper);
            return R.success("修改成功！");
        }

        return R.error("修改失败,该菜品已存在！");
    }

    /**
     * 删除菜品种类
     * @param ids
     * @return
     */
    @DeleteMapping()
    public R<String> delete(Long ids) {
        R<String> r = categoryService.remove(ids);
        return r;
    }
    @GetMapping("list")
    public R<List> list(Integer type){
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        if(type!= null){
            wrapper.eq(Category::getType,type);
        }
        List<Category> list = categoryService.list(wrapper);
        return R.success(list);
    }
}
